package cn.nange.cmfz.service.impl;

import cn.nange.cmfz.service.UserService;
import cn.nange.cmfz.service.UserThreadService;
import cn.nange.cmfz.util.ThreadPoolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;

@Service
@Transactional
public class UserThreadServiceImpl implements UserThreadService {
    @Autowired
    private UserService userService;

    @Override
    public Map showAllCountThread() throws InterruptedException {
        Map map = new HashMap<>();
//        1.创建计数器
        CountDownLatch countDownLatch = new CountDownLatch(3);

//        ThreadPoolUtil.executor.execute(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    Map map1 = userService.showByDayCount();
//                    map.put("count", map1);
//                    countDownLatch.countDown();
//                } catch (Exception e) {
//                    countDownLatch.countDown();
//                }
//            }
//
//        });

        ThreadPoolUtil.executor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Map map1 = userService.showByDay();
                    map.put("count", map1);
                    countDownLatch.countDown();
                } catch (Exception e) {
                    countDownLatch.countDown();
                }
            }

        });

        ThreadPoolUtil.executor.execute(new Runnable() {
            @Override
            public void run() {
                //                查询注册量

                List<Map> maps = userService.showAllBySex();
                map.put("sex", maps);
                //                计数器减一
                countDownLatch.countDown();

            }
        });

        ThreadPoolUtil.executor.execute(new Runnable() {
            @Override
            public void run() {
                List<Map> maps = userService.showAllByProvince();
                map.put("province", maps);

                countDownLatch.countDown();
            }
        });

        countDownLatch.await();

        return map;
    }

//    @Override
////    public Map showAllCountFuture() throws Exception {
////
////        Map map = new HashMap<>();
////
////        Future<String> count = ThreadPoolUtil.executor.submit(new Callable<String>() {
////            @Override
////            public String call() throws Exception {
////                Map map1 = userService.showByDayCount();
////                map.put("count", map1);
////                return "done";
////            }
////        });

    @Override
    public Map showAllCountFuture() throws Exception {

        Map map = new HashMap<>();

        Future<String> count = ThreadPoolUtil.executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                Map map1 = userService.showByDay();
                map.put("count", map1);
                return "done";
            }
        });

        Future<String> sex = ThreadPoolUtil.executor.submit(new Callable<String>() {

            @Override
            public String call() throws Exception {
                List<Map> maps = userService.showAllBySex();
                map.put("sex", maps);
                return "done";
            }
        });

        Future<String> province = ThreadPoolUtil.executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                List<Map> maps = userService.showAllByProvince();
                map.put("province", maps);
                return "done";
            }
        });

        if ("done".equals(sex.get()) && "done".equals(province.get()) && "done".equals(count.get())) {
            return map;
        }

        return map;
    }

    /**
     * 没有优化  串行执行
     *
     * @return
     */
    @Override
    public Map showAllCount() {
        Map map = new HashMap<>();
        //        男女比例
        List<Map> maps = userService.showAllBySex();
        map.put("sex", maps);
        //        地区分布
        List<Map> maps1 = userService.showAllByProvince();
        map.put("province", maps1);
        //        注册量
//        Map map1 = userService.showByDayCount();
        Map map1 = userService.showByDay();
        map.put("count", map1);
        return map;
    }
}
